<?php

/**
 * This class manages all database queries (or other operations)
 *  concerning children.
 */
class Model_Parent extends Model_Person
{

    /**
     * Lists all parent, ordered alphabetically by last name.
     * 
     * @return array an array of (associative) arrays
     */
    public function listAll($where = "person_type = 'Elternteil'")
    {
	return parent::listAll($where);
    }

    /**
     * Persists a new parent to the database.
     * 
     * @param array $parent the child's data
     */
    public function create($parent)
    {
	$parent['person_type'] = 'Elternteil';
	$this->db->insert($this->name, $parent);
	$new_id = $this->db->lastInsertID();
	return ($new_id);
    }

    /**
     * Updates a parent's data.
     * 
     * @param array $parent the parent's data
     */
    public function update(array $parent)
    {
	
	$parent['person_type'] = 'Elternteil';
	parent::update($parent);
    }

    /**
     * Removes a parent from the database identified by its id.
     * 
     * @param type $id the parent's id
     */
    public function deleteAll()
    {
	$this->db->delete($this->name, "person_status = 'Elternteil'");
    }
    
     /**
     * Removes a parent from the database identified by its id.
     * 
     * @param type $id the parent's id
     */
    public function delete($id)
    {
	$where = $this->db->quoteInto('id = ?', $id);
	$this->db->delete($this->name, $where);
    }

    /**
     * Removes a parent from the database identified by its id.
     * 
     * @param type $id the parent's id
     */
    public function findByIdWithAddress($id)
    {
	$select = $this->db->select()
		->from(array('p'=>'person'))
		->joinRight(array('a'=>'address'), 'p.idaddress = a.id', array('city','street', 'housenumber','postalcode'))
		->where('p.id = ?', $id);
	
	return $this->db->fetchRow($select);
    }

       public function listAllParent()
    {	
	   // "surname||' '||forename"
	//Nachname Vorname muss noch verknüpft werden für ansicht in dropdown
	$select = $this->db->select('id','forename','surname')
		->from(array('person'),
			array('key' => 'id', 'value' => 'surname'))
		->order('surname ASC')
		->where('person_type = ?', 'Elternteil');

	return $this->db->fetchAll($select);
    }
    
    public function listParentsOfChildWithId($child_id)
    {
	$plp=new Model_PersonLinkPerson;
	return $plp->findParents($child_id);
    }
    
}